﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace MyValidationLibrary
{
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
	public class BaseValidationAttribute : Attribute
	{
		public BaseValidationAttribute(string mes)
			: base()
		{
			ErrorMessage = mes;
		}

		public string ErrorMessage
		{
			get;
			set;
		}
	}

	public class StringLenghtAttribute : BaseValidationAttribute
	{
		public StringLenghtAttribute(int requiredLenght, string error)
			: base(error)
		{
			Lenght = requiredLenght;
			ErrorMessage = error;
		}

		public int Lenght
		{
			get;
			set;
		}
	}

	public class RangeAttribute : BaseValidationAttribute
	{
		public RangeAttribute(int min, int max, string error)
			: base(error)
		{
			this.Min = min;
			this.Max = max;
			ErrorMessage = error;
		}

		public int Min
		{
			get;
			set;
		}

		public int Max
		{
			get;
			set;
		}
	}

	[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
	public class DefaultValueAttribute : Attribute
	{
		public DefaultValueAttribute(object val)
			: base()
		{
			DefaultValue = val;
		}

		public Object DefaultValue
		{
			get;
			set;
		}
	}


	public class RequiredAttribute : BaseValidationAttribute
	{
		public RequiredAttribute(string message)
			: base(message)
		{
			Required = true;
		}

		public RequiredAttribute(bool required, string message)
			: base(message)
		{
			Required = required;
		}

		public bool Required
		{
			get;
			set;
		}
	}


	public class RegexValidationAttribute : BaseValidationAttribute
	{
		public RegexValidationAttribute(string RegexString, string ErrorMessage)
			: base(ErrorMessage)
		{
			this.RegexString = RegexString;
		}

		public string RegexString { get; set; }

		public string IsValid(PropertyInfo property, object Object)
		{
			object Value = property.GetValue(Object, null);
			if (Value is String)
			{
				System.Text.RegularExpressions.Regex TempRegex = new System.Text.RegularExpressions.Regex(RegexString);
				if (TempRegex.IsMatch((string)Value))
					return "";
			}
			return ErrorMessage;
		}
	}


}
